package demo;

import java.util.Objects;

class Student1{
    public String name;
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }
  /*  public boolean equals(Object obj) {
        if(obj == null) {
            return false;
        }
        //指向的是同一个对象
        if(this == obj){
            return true;
        }
        //判断两个对象是否为同一个类型
        if(!(obj instanceof Student1)){
            return false;
        }
        Student1 student =(Student1) obj;
        if(this.name.equals(student.name)){
            return true;
        }
            return false;
    }*/

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student1 student1 = (Student1) o;
        return Objects.equals(name, student1.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }
}
public class Test2 {
    public static void main(String[] args) {
        Student1 student = new Student1();
        student.name = "zhangsan";

        Student1 student2 = new Student1();
        student2.name = "zhangsan";
        boolean flg = student.equals(student2);
        System.out.println("flg:"+flg);
    }
}
